﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Phone.Info;
using DDU.Enums;
using DDU.Library.Helpers;
using System.Globalization;
using System.Windows.Input;
using DDU.Library;
using System.Windows;

namespace DDU.ViewModel
{
    public class UnlockViewModel : ViewModelBase
    {
        public string MachineCode
        {
            get
            {
                //string imei = "359289042687448";
                
                byte[] uniqueId = DeviceExtendedProperties.GetValue("DeviceUniqueId") as byte[];

                string uniqueString = Convert.ToBase64String(uniqueId).ToUpper();
                for (var i = 0; i < uniqueString.Length;)
                {
                    char _char = uniqueString[i];
                    if (!System.Char.IsLetter(_char) && !System.Char.IsNumber(_char))
                    {
                        uniqueString = uniqueString.Remove(i, 1);
                    }
                    else
                    {
                        i++;
                    }
                }
                for (var i = 1; i < 5; i++)
                {
                    uniqueString = uniqueString.Insert(i * 6 - 1, "-");
                }

                return uniqueString;
            }
        }

        private string orderNumber = string.Empty;
        public string OrderNumber
        {
            get { return orderNumber; }
            set
            {
                orderNumber = value;
                RaisePropertyChanged(() => OrderNumber);
                RaisePropertyChanged(() => ResolveCommand);
            }
        }

        private UnlockPaymentWayValue payway = UnlockPaymentWayValue.Alipay;
        public UnlockPaymentWayValue Payway
        {
            get { return payway; }
            set
            {
                payway = value;
                RaisePropertyChanged(() => Payway);
            }
        }

        public ICommand ResolveCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return OrderNumber.Trim().Length > 5; },
                    ExecuteCallback = delegate { }
                };
            }
        }

        public ICommand CopyCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Clipboard.SetText(MachineCode);
                    }
                };
            }
        }
    }
}
